/**
 * author:huangwq, version:1.0, 对话框层,使用需引入css：css/chongqing/popup.css
 */
// width:对话框宽度
// height:对话框高度
// innerDivId:需要嵌入对话框的div的id
// onDialogClose：点击关闭按钮触发的函数，可以直接提供方法名或以function(){...}的形式提供
// 添加参数needNotCloseObj：是否需要关闭遮罩层的按钮(bim)
var DialogDiv = function(width, height, innerDivId, onDialogClose, needNotCloseObj, needNotBackgroud) {
  this.wrapperTargetDivId = innerDivId;
  var _w = parseInt(width);
  var _h = parseInt(height);
  this.width = _w <= 0 ? 630 : _w;
  this.height = _h <= 0 ? 300 : _h;
  this.onDialogClose = onDialogClose;
  this.needNotCloseObj = needNotCloseObj;
  this.needNotBackgroud = needNotBackgroud;
  // this.maskOnTop = true;// 遮盖的透明层显示在top窗口里
};

DialogDiv.prototype = {
  hide : function() {// 隐藏对话框
    this.dialogDiv.style.display = "none";
  },

  show : function() {// 显示对话框
    var success = this._init();
    if (success) {
      var wrapperTargetDiv = document.getElementById(this.wrapperTargetDivId);
      wrapperTargetDiv.style.display = "";
      this.dialogDiv.style.display = "";
      if (_browserUtils.isIE6()) {
        var locateDiv = this.dialogDiv.getElementsByTagName("DIV")[1];
        locateDiv.style.marginTop = document.documentElement.scrollTop - 217 + "px";
      }
    }
  },

  _init : function() {
    if (this.dialogDiv) {// 已创建
      return true;
    }
    if (!document.getElementById(this.wrapperTargetDivId)) {
      alert("Can't init DialogDiv:innerDivId prameter invalid,can't find the id:" + this.wrapperTargetDivId
          + " in html document");
      return false;
    }

    // 如果之前已经对该innerDivId创建过DialogDiv，则使用已有的对象
    var newId = "DialogDiv_for_" + this.wrapperTargetDivId;
    if (document.getElementById(newId)) {
      this.dialogDiv = document.getElementById(newId);
      return true;
    }
    var dialogDiv = document.createElement('div');

    this.dialogDiv = dialogDiv;
    this.dialogDiv.id = newId;

    this.wrapperDivObj = _dialogUtils._createWrapper(this, this.width, this.height, this.dialogDiv, this.onDialogClose,
        this.needNotCloseObj, this.needNotBackgroud);

    document.body.appendChild(dialogDiv);
    var wrapperTargetDiv = document.getElementById(this.wrapperTargetDivId);
    this.wrapperDivObj.appendChild(wrapperTargetDiv);
    return true;
  }
};

// 对话框工具类，内部调用
var _dialogUtils = {
  // 给对话框层附加外围装饰
  _createWrapper : function(target, width, height, dialogDivObj, onDialogClose, needNotCloseObj, needNotBackgroud) {
    dialogDivObj.style.display = "none";
    dialogDivObj.appendChild(this._newHtmlObj('div', 'dl_mask'));
    var popup_wrapper = this._newHtmlObj('div', 'popup_wrapper');
    popup_wrapper.style.width = width + 'px';
    popup_wrapper.style.height = height + 'px';
    dialogDivObj.appendChild(popup_wrapper);
    var popup_outer = this._newHtmlObj('div', 'popup_outer');
    popup_wrapper.appendChild(popup_outer);
    if (!needNotBackgroud) {
      popup_outer.appendChild(this._newHtmlObj('div', 'bg_n'));
      popup_outer.appendChild(this._newHtmlObj('div', 'bg_ne'));
      popup_outer.appendChild(this._newHtmlObj('div', 'bg_e'));
      popup_outer.appendChild(this._newHtmlObj('div', 'bg_se'));
      popup_outer.appendChild(this._newHtmlObj('div', 'bg_s'));
      popup_outer.appendChild(this._newHtmlObj('div', 'bg_sw'));
      popup_outer.appendChild(this._newHtmlObj('div', 'bg_w'));
      popup_outer.appendChild(this._newHtmlObj('div', 'bg_nw'));
      var popup_inner = this._newHtmlObj('div', 'popup_inner');
      popup_outer.appendChild(popup_inner);
    } else {
      var popup_inner = this._newHtmlObj('div', '');
      popup_outer.appendChild(popup_inner);
    }
    if (!needNotCloseObj) {
      var a = this._newHtmlObj('a', 'close');
      // a.setAttribute("onclick", onDialogClose);
      if (onDialogClose == undefined) {
        a.onclick = function() {
          target.hide();
        };
      } else {
        a.onclick = onDialogClose;
      }
      popup_outer.appendChild(a);
    }
    return popup_inner;
  },

  _newHtmlObj : function(tagName, className) {
    var obj = document.createElement(tagName);
    // obj.setAttribute("class", className);//这个在IE下不起作用，杯具,用下面这句代替
    obj.className = className;
    return obj;
  }
};

var _browserUtils = {
  isIE6 : function() {
    if (window.XMLHttpRequest) {// 判断浏览器是不是归属Mozilla,Sofari
      return false;// firefox
    } else if (window.ActiveXObject) {// 判断浏览器是不是归属IE
      var browser = navigator.appName;
      var b_version = navigator.appVersion;
      var version = b_version.split(";");
      var trim_Version = version[1].replace(/[ ]/g, "");
      if (browser == "Microsoft Internet Explorer" && trim_Version == "MSIE7.0") {
        return false// IE 7.0
      } else if (browser == "Microsoft Internet Explorer" && trim_Version == "MSIE6.0") {
        return true;
      }
    }
  }
};